<%-- 
    Document   : changeRoom
    Created on : Jul 10, 2013, 3:45:53 PM
    Author     : Anna SAELEE
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link type="text/css" rel="stylesheet" href="style.css"/>
        <title>S'incrire un étudiant à une chambre</title>
    </head>
    <%  Connection conn;
        Statement stmt;
        ResultSet rs, rs2;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dormitorymanagement", "root", "root");
            stmt = conn.createStatement();%>
    <body>
        <div class="wrapper">
            <div class="header"></div>
            <div class="navigator"> <a href="#">MENU</a> > <a href="#">SUBMENU</a> 30*1000 </div>
            <div class="workspace">
                <%@include file="connect.jsp" %>
                <div class="workspace2">
                    <% if (session.getAttribute("username") == null || !session.getAttribute("username").equals("admin")) {%>
                    Connectez-vous comme l'administrator, s'il vous plaît
                    <% } else if (session.getAttribute("username").equals("admin")) {
                        String firstname = "";
                        String lastname = "";
                        String r_number = "";
                        String selectedUsr = request.getParameter("selectedUsr");
                        String sql = "SELECT firstname, lastname, r_number FROM account WHERE username='" + selectedUsr + "'";
                        rs = stmt.executeQuery(sql);
                        while (rs.next()) {
                            firstname = rs.getString("firstname");
                            lastname = rs.getString("lastname");
                            r_number = rs.getString("r_number");
                        }
                    %>
                    <h3>Change la chambre</h3></br>
                    <table width="100%" border="0" align="center">
                        <tr>
                            <th colspan="3"><%= firstname%> <%= lastname%></th>
                        </tr>
                        <tr style="text-align: center">
                            <th>Actuelle Chambre: </th>
                            <td colspan="2"><%= r_number%></td>
                        </tr>
                        <tr style="text-align: center">
                            <th>Nouvelle Chambre: </th>
                                <% String sql_room = "SELECT r_number FROM room WHERE available != 0";
                                    rs2 = stmt.executeQuery(sql_room);
                                    ArrayList<String> roomList = new ArrayList<String>();
                                    while (rs2.next()) {
                                        roomList.add(rs2.getString("r_number"));
                                    }
                                %>
                        <form name="changeRoom" method="post" action="Update">
                            <input type="hidden" name="typeMss" value="changeRoom"/>
                            <input type="hidden" name="selectedUsr" value="<%= selectedUsr%>"/>
                            <input type="hidden" name="oldroom" value="<%= r_number%>"/>
                            <td style="text-align: center">
                                <select name="room">
                                    <option>Choisir une chambre..</option>
                                    <% for (int i = 0; i < roomList.size(); i++) {
                                            out.println("<option value=\"" + roomList.get(i) + "\">" + roomList.get(i) + "</option>");
                                        }
                                    %>
                                </select>
                            </td>
                            <td style="text-align: center">
                                <input type="submit" name="doChangeRoom" value="Mettre à jour le changement"/>
                            </td>
                        </form>
                        </tr>
                        <% }%>
                    </table>
                </div>
                <%@include file="sidespace.jsp" %>
            </div>
            <div class="footer"> FOOTER 80*1000 </div>
        </div>
    </body>
    <% } finally {
            out.close();
        }
    %>
</html>
